package com.example.demo.controller;

import com.example.demo.Result;
import com.example.demo.pojo.Article;
import com.example.demo.service.ArticleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "添加文章",
            notes = "输入标题，正文，类型id添加文章",
            httpMethod ="POST",
            produces = "application/json",
            consumes = "applicaiton/json",
            response = Result.class
    )
    @PostMapping
    public Result add(@RequestBody Article article) {
        articleService.saveOrUpdate(article);
        return Result.ok("200","success", article);
    }

    @ApiOperation(value = "删除文章",
            notes = "根据文章的id删除文章",
            httpMethod ="DELETE"
    )
    @ApiImplicitParams(
        {
                @ApiImplicitParam(name = "id",value = "文章的id",required = true,example = "1")
        }
    )
    @DeleteMapping("/{id}")
    public Object del(@PathVariable Integer id) {
        articleService.delete(id);
        return "success";
    }

    @PutMapping("/{id}")
    public Object update(@PathVariable Integer id, Article article) {
        article.setId(id);
        articleService.saveOrUpdate(article);
        return article;
    }

    @GetMapping("/{id}")
    public Object get(@PathVariable Integer id) {
        return articleService.get(id);
    }

    @GetMapping
    public Object getAll() {
        return articleService.getAll();
    }
}
